/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ruoyi.common.adapter;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.apache.commons.lang3.StringUtils;
import static org.apache.commons.lang3.StringUtils.endsWithIgnoreCase;
import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
import static org.apache.commons.lang3.StringUtils.startsWithIgnoreCase;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 *
 * @author wmao
 */
public class CDataAdapter extends XmlAdapter<String, String> {

    @Override
    public String unmarshal(String vt) throws Exception {
        if (equalsIgnoreCase("<![CDATA[]]", vt)) {
            return "";
        } else {
            String subStart = "<![CDATA[";
            if (startsWithIgnoreCase(vt, subStart)) {
                vt = StringUtils.substringAfterLast(vt, subStart);
            }
            String subEnd = "]]>";
            if (endsWithIgnoreCase(vt, subEnd)) {
                vt = StringUtils.substringBefore(vt, subEnd);
            }
            return vt;
        }
    }

    @Override
    public String marshal(String bt) throws Exception {
        return isNotBlank(bt) ? "<![CDATA[" + bt + "]]>" : bt;
    }

}
